﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MAT.Model;
 
namespace MAT.Controllers
{
    class ctrRequestAssetLiquidation
    {
        private frmRequestAssetLiquidation _frmRequestAssetLiquidation = null;

        public frmRequestAssetLiquidation FrmRequestAssetLiquidation
        {
            get { return _frmRequestAssetLiquidation; }
            set { _frmRequestAssetLiquidation = value; }
        }

        MAT_Context context = MATContext.getContext();

        private String getRadioButtonText(GroupBox a)
        {
            String result = "";
            foreach (Control c in a.Controls)
            {
                RadioButton rb = c as RadioButton;
                if (rb.Checked)
                    result = rb.Text;
            }
            return result;
        }

        private bool check()
        {
            if (_frmRequestAssetLiquidation.txtRALReason.Text.TrimStart().Length < 1 || _frmRequestAssetLiquidation.txtRALReason.Text.TrimStart().Length > 200)
            {
                MessageBox.Show("Your reason is from 1 to 200 digits");
                _frmRequestAssetLiquidation.txtRALReason.Focus();
                return false;
            }
            if (getRadioButtonText(_frmRequestAssetLiquidation.grbFELiquidation) == "")
            {
                MessageBox.Show("You don't choose the FE Liquidation in the list of choices");
                _frmRequestAssetLiquidation.grbFELiquidation.Focus();
                return false;
            }
            else
                return true;
        }

        public void frmRequestAssetLiquidation_Load()
        {
            try
            {
                _frmRequestAssetLiquidation.cmbRALAssetCategory.DataSource = context.CATEGORies.ToList();
                _frmRequestAssetLiquidation.cmbRALAssetCategory.DisplayMember = "CATEGORY_NAME";
                _frmRequestAssetLiquidation.cmbRALAssetCategory.ValueMember = "CATEGORY_ID";
                _frmRequestAssetLiquidation.txtRequestDate.Text = DateTime.Now.ToString();
            }
            catch { }
        }

        public void cmbRALAssetCategory_SelectedIndexChanged()
        {
            try
            {
                string a = _frmRequestAssetLiquidation.cmbRALAssetCategory.SelectedValue.ToString();
                List<ASSET> assets = new List<ASSET>();
                assets = context.ASSETs.Where(ass => ass.CATEGORY_ID == a).ToList();
                if (assets.Count != 0)
                {
                    _frmRequestAssetLiquidation.cmbRALAssetName.DataSource = assets.ToList();
                    _frmRequestAssetLiquidation.cmbRALAssetName.DisplayMember = "ASSET_NAME";
                    _frmRequestAssetLiquidation.cmbRALAssetName.ValueMember = "ASSET_ID";
                    _frmRequestAssetLiquidation.cmbRALAssetName.Enabled = true;
                }
                if (assets.Count == 0)
                {
                    _frmRequestAssetLiquidation.cmbRALAssetName.Enabled = false;
                    _frmRequestAssetLiquidation.txtRALBrand.Text = "";
                    _frmRequestAssetLiquidation.txtRALBarcode.Text = "";
                    _frmRequestAssetLiquidation.txtRALModel.Text = "";
                    _frmRequestAssetLiquidation.txtRALSupplier.Text = "";
                    _frmRequestAssetLiquidation.txtRALSupAddress.Text = "";
                    _frmRequestAssetLiquidation.txtRALDepartment.Text = "";
                    _frmRequestAssetLiquidation.txtRALClassified.Text = "";
                }
            }
            catch { }
        }

        public void cmbRALAssetID_SelectedIndexChanged()
        {
            try
            {
                string aID = _frmRequestAssetLiquidation.cmbRALAssetName.SelectedValue.ToString();
                ASSET asset = context.ASSETs.FirstOrDefault(ass => ass.ASSET_ID == aID);
                if (asset != null)
                {
                    _frmRequestAssetLiquidation.txtRALBrand.Text = asset.BRAND;
                    _frmRequestAssetLiquidation.txtRALBarcode.Text = asset.BARCODE;
                    _frmRequestAssetLiquidation.txtRALModel.Text = asset.MODEL_NUM;

                    //View data of ROOM depend on Employee ID
                    string eID = asset.EMPLOYEE_ID.ToString();
                    OWNER own = context.OWNERS.FirstOrDefault(o => o.EMPLOYEE_ID == eID);
                    ROOM room = context.ROOMs.FirstOrDefault(r => r.ROOM_ID == own.ROOM_ID);
                    _frmRequestAssetLiquidation.txtRALDepartment.Text = room.ROOM_NAME;
                    _frmRequestAssetLiquidation.txtRALClassified.Text = room.CLASSIFIED.ToString();

                    //View data of SUPPLIER depend on Employee ID
                    string sID = asset.SUPPLIER_ID.ToString();
                    SUPPLIER supplier = context.SUPPLIERs.FirstOrDefault(sup => sup.SUPPLIER_ID == sID);
                    _frmRequestAssetLiquidation.txtRALSupplier.Text = supplier.SUPPLIER_NAME;
                    _frmRequestAssetLiquidation.txtRALSupAddress.Text = supplier.SUPPLIER_ADDRESS;

                }
                _frmRequestAssetLiquidation.btnRALSend.Enabled = true;
            }
            catch { }
        }

        public void btnRALClose_Click()
        {
            _frmRequestAssetLiquidation.Close();
            MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
        }

        public void btnRALReset_Click()
        {
            _frmRequestAssetLiquidation.txtRALBarcode.Text = "";
            _frmRequestAssetLiquidation.txtRALBrand.Text = "";
            _frmRequestAssetLiquidation.txtRALClassified.Text = "";
            _frmRequestAssetLiquidation.txtRALDepartment.Text = "";
            _frmRequestAssetLiquidation.txtRALModel.Text = "";
            _frmRequestAssetLiquidation.txtRALReason.Text = "";
            _frmRequestAssetLiquidation.txtRALSupAddress.Text = "";
            _frmRequestAssetLiquidation.txtRALSupplier.Text = "";
            _frmRequestAssetLiquidation.cmbRALAssetCategory.Text = "";
            _frmRequestAssetLiquidation.cmbRALAssetName.Text = "";
        }

        public void btnRALSend_Click()
        {
            if (check())
            {
                REQUEST_LIQUIDATION request = new REQUEST_LIQUIDATION();
                request.ASSET_ID = _frmRequestAssetLiquidation.cmbRALAssetName.SelectedValue.ToString();
                request.REQUEST_DATE = DateTime.Now;
                request.DISPOSAL_REQ = getRadioButtonText(_frmRequestAssetLiquidation.grbFELiquidation);
                request.EMPLOYEE_ID = MAT.Controllers.ctrLogin.employee_ID;
                request.REASON = _frmRequestAssetLiquidation.txtRALReason.Text;
                if (MAT.Controllers.ctrLogin.priority == 3 || MAT.Controllers.ctrLogin.priority == 2)
                    request.REQ_STATUS = "RE001";
                else
                    request.REQ_STATUS = "RE003";
                try
                {
                    context.REQUEST_LIQUIDATION.AddObject(request);
                    context.SaveChanges();
                    MessageBox.Show("SUCCESSFUL!");
                    btnRALReset_Click();
                }
                catch { }
            }
        }

    }
}
